#include <QtGui/QAction>
#include <QtGui/QDockWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QTextBrowser>
#include <QyDebug.h>
#include "Qsi.h"

using namespace Qsi;

/**************************************************************************************************/

Debugger::Debugger( QScriptEngine * engine )
	: QScriptEngineAgent(engine), p_lineDelay(100), p_lineDelayEnabled(false)
{

// 	if (engine) {
// 	}

}

void Debugger::positionChange( qint64 scriptId, int lineNumber, int columnNumber ) {

	emit positionChanged( lineNumber, columnNumber );

}

void Debugger::exceptionCatch( qint64 scriptId, const QScriptValue & exception ) {

	emit exeptionThrowed(exeption);

}

void Debugger::exceptionThrow( qint64 scriptId, const QScriptValue & exception,
	bool hasHandler )
{

	emit exeptionCatched( exeption, hasHandler );

}

void Debugger::functionEntry( qint64 scriptId ) {

	emit functionCalled();

}

void Debugger::functionExit( qint64 scriptId, const QScriptValue & returnValue ) {

	emit functionFinished(returnValue);

}

void Debugger::scriptLoad( qint64 id, const QString & program, const QString & fileName,
	int baseLineNumber )
{

	emit scriptLoaded( fileName, baseLineNumber );

}

void Debugger::scriptUnload( qint64 id ) {

	emit scriptUnloaded();

	if ( QScriptEngine * engine = Debugger::engine() ) {

		if ( engine->hasUncaughtException() ) {

			QStringList backtrace = engine->uncaughtExceptionBacktrace();
			QScriptValue exeption = engine->uncaughtException();

			emit error( QString("%1\n%2\n\n").arg( exeption.toString() )
				.arg( backtrace.join("\n") ) );

		}

	}

}

/**************************************************************************************************/

Interpreter::Interpreter( QObject * parent = 0 )
	: QObject(parent), debugger(&engine)
{



}

Interpreter::~Interpreter() {
}

bool Interpreter::evaluateFile( const QString & fileName ) {

	engine.pushContext();
// 	engine->globalObject().setProperty( "interpreter", engine->newQObject(this) );
// 	engine->globalObject().setProperty( "MouseLeftButton", Qt::LeftButton );
// 	engine->globalObject().setProperty( "MouseMidButton", Qt::MidButton );
// 	engine->globalObject().setProperty( "MouseRightButton", Qt::RightButton );

	QFile file(fileName);

	if ( !file.open(QFile::ReadOnly) )
		return false;

	QByteArray data = file.readAll();
	file.close();

    /*QScriptValue result = */engine.evaluate( data, fileName );

// 	if ( engine.hasUncaughtException() ) {

// 		QStringList backtrace = engine->uncaughtExceptionBacktrace();

// 		print( QString("  %1\n%2\n\n").arg( result.toString() ).arg( backtrace.join("\n") ) );

// 	}
	engine->popContext();

	return true;
}

bool Interpreter::registerObject( QObject * object, const QString & name ) {

	engine.globalObject().setProperty( name, engine.newQObject(object) );

}
